/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.dialogs;


import java.util.Iterator;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.uml2.uml.BehavioredClassifier;
import org.eclipse.uml2.uml.Operation;

import com.cea.papyrus.core.PapyrusPlugin;

/**
 * Dialog to select an Operation of a BehavioredClassifier.
 * @deprecated see {@link ElementTreeSelectionDialog}
 */
public class OperationTreeSelectionDialog extends Dialog {

	/**
	 * The BehavioredClassifier for which we want to show a list of operations.
	 */
	protected BehavioredClassifier behavioredClassifier;

	/**
	 * Chosen operation.
	 */
	protected Operation operation;

	/**
	 * The selection tree.
	 */
	protected Tree tree;

	/**
	 * Constructor with parent shell and behavioredClassifier.
	 * 
	 * @param parent the Shell.
	 * @param behavioredClassifier the BehavioredClassifier.
	 */
	public OperationTreeSelectionDialog(Shell parent, BehavioredClassifier behavioredClassifier) {
		super(parent);
		this.behavioredClassifier = behavioredClassifier;
		// add the resize ability to the window
		setShellStyle(SWT.RESIZE | super.getShellStyle());
	}

	/**
	 * Creates the dialog area.
	 * 
	 * @param parent composite.
	 * 
	 * @return control Control.
	 */
	protected Control createDialogArea(Composite parent){
		Composite composite = (Composite) super.createDialogArea(parent);
		tree = new Tree(composite, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL | SWT.RESIZE);

		//build tree item for each Operation contained in the BehavioredClassifier
		Iterator iter = behavioredClassifier.getAllOperations().iterator();
		while(iter.hasNext()){
			Operation currentOperation = (Operation)iter.next();
			TreeItem item = new TreeItem(tree, SWT.NONE);
			
			// Prepare operation label
			String label = com.cea.papyrus.umlutils.Operation.getSignature(currentOperation, true);
						
			item.setText(label);
			item.setData(currentOperation);
			item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(currentOperation));
		}

		tree.setLayoutData(new GridData(550, 300));

		tree.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e){
				TreeItem[] selection = tree.getSelection();
				if(selection[0].getData() instanceof Operation){
					operation = (Operation)selection[0].getData();
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
		return composite;
	}


	/**
	 * Returns the associated Operation.
	 * 
	 * @return operation Operation
	 */
	public Operation getResult(){
		return operation;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
	 */
	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText(behavioredClassifier.getName()+": Operation Selection");
	}

}
